use "$LocalData\BartikAnalyticData_2digit.dta", clear

* Set panel variables
xtset geography year

*Generating year-industry-shares for computation for Bartik analytics and SEs
		keep if year>2002  //Restricts to period of analysis
		
		levelsof year, local(y) //Creates a macro list of all years
		
		foreach year in `y' {
			foreach ind_var of varlist c_i_share_2000* {
				qui gen t`year'_`ind_var' = `ind_var' * (year == `year')
				}
			}
	
		qui tab year, gen(year_) //Generates year dummies
		drop year_1
		qui tab geography, gen(geography_) //Generates year dummies
		drop geography_1
		

/******************************************************************************/
/******************************************************************************/
/* Regressions                                                                */
/******************************************************************************/
/******************************************************************************/

local years  (year>2002) & abs(gsmall_dep1)<5 & gSmallFirm1!=.
*local controls ln_pop_2000 unem_2000 urban_2000 lnincome2000 lnbr2000 small_dep_share2000
local controls 	dlngdp unem_change pop_growth annualchange 

gen testconditions = cond(`years',1,0)
bysort geography: egen insample = sum(test)
keep if insample>1

/**********************/
/* Simple Regressions */
/**********************/
noi display "Running simple regressions."

local years  (year>2002) & abs(gsmall_dep1)<5 & gSmallFirm1!=.
*local controls ln_pop_2000 unem_2000 urban_2000 lnincome2000 lnbr2000 small_dep_share2000
local controls 	dlngdp unem_change pop_growth annualchange 


sort geography
merge m:1 geography using "$SourceData\ChinaShock.dta"
sort geography year


*Opening file to collect standard errors from Adao et al 2019
capture file close fh
file open fh  using "$LocalData\Tables\Adao_Standard_Errors_TableNoMiningRFS.tex", write replace
file write fh "\toprule" _n
file write fh  " (1) & (2)  & (3) & (4) & (5) & (6) & (7) & (8) & (9)" _n


noi display "Starting baseline regressions."
noi display " "

*Deposits
noi display "Deposit regressions."
local years  (year>2002) & abs(gsmall_dep1)<5  & !missing(lnincome2000) & !missing(bartikGS1) & !missing(gSmallFirm1) & c_i_share_200021==0
*& L.small_dep_share>0 & L.small_br_share>0

eststo: xtivreg2 gsmall_dep1 year_* (gSmallFirm1 = bartikGS1)  if `years', fe cluster(cluster3)


gen mainsample = e(sample)
bysort geography: egen singletons = sum(mainsample)
replace mainsample=. if singletons<2
drop singletons
local years mainsample==1

*(1)

eststo: areg gsmall_dep1 gSmallFirm1 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_TableNoMiningRFS", tex replace ///
	ctitle(Small Dep Gr) keep(gSmallFirm1 `controls' ) ///
	addtext(REG, OLS, F-stat, N/A, YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)
	
*(2)
eststo: areg gSmallFirm1 bartikGS1 year_*  if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_TableNoMiningRFS", tex append ///
	ctitle(Small Dep Gr) keep(gSmallFirm1 bartikGS1 gLargeFirm1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, N/A, YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)

*(3)
eststo: xtivreg2 gsmall_dep1 year_* (gSmallFirm1 = bartikGS1)  if `years', fe cluster(cluster3)
local widstat = round(`e(widstat)',0.1)
outreg2 using "$LocalData\Tables\BartikRegTable_TableNoMiningRFS", tex append ///
	ctitle(Small Dep Gr) keep(gSmallFirm1  gLargeFirm1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)

	preserve
	

		keep if `years'
		drop t*_c_i_share_200021
		local controls2 geography_* year_*
		local weightmatrix "t*_c_i_share_2000*"
		local filter `years'
		
		noi Adao dsmall_dep_share1 bartikS1 `controls2' if `filter', wmatrix(`weightmatrix') tsls(gSmallFirm1)

		local se2 =string(se0[1,1], "%9.3f")
		local ciL2 =string(ciL[1,1], "%9.3f")
		local ciH2 =string(ciH[1,1], "%9.3f")

		local se3 =string(se_alpha[1,1], "%9.3f")
		local ciL3 =string(ciL2SLS[1,1], "%9.3f")
		local ciH3 =string(ciH2SLS[1,1], "%9.3f")
	restore



egen medmanuf = pctile(c_i_share_200031)	
	
local years  (year>2002) & abs(gsmall_dep1)<5  & !missing(lnincome2000) & !missing(bartikGS1) & !missing(gSmallFirm1) & c_i_share_200031<medmanuf
local years  (year>2002) & abs(gsmall_dep1)<5  & !missing(lnincome2000) & !missing(bartikGS1) & !missing(gSmallFirm1) &  cuts<4

*c_i_share_200031<medmanuf

eststo: xtivreg2 gsmall_dep1 year_* (gSmallFirm1 = bartikGS1)  if `years', fe cluster(cluster3)
drop mainsample
gen mainsample = e(sample)
local years mainsample==1
bysort geography: egen singletons = sum(mainsample)
replace mainsample=. if singletons<2
drop singletons
local years mainsample==1

*(4)

eststo: areg gsmall_dep1 gSmallFirm1 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_TableNoMiningRFS", tex append ///
	ctitle(Small Dep Gr) keep(gSmallFirm1 `controls' ) ///
	addtext(REG, OLS, F-stat, N/A, YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)
	
*(5)
eststo: areg gSmallFirm1 bartikGS1 year_*  if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_TableNoMiningRFS", tex append ///
	ctitle(Small Dep Gr) keep(gSmallFirm1 bartikGS1 gLargeFirm1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, N/A, YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)

*(6)
eststo: xtivreg2 gsmall_dep1 year_* (gSmallFirm1 = bartikGS1)  if `years', fe cluster(cluster3)
local widstat = round(`e(widstat)',0.1)
outreg2 using "$LocalData\Tables\BartikRegTable_TableNoMiningRFS", tex append ///
	ctitle(Small Dep Gr) keep(gSmallFirm1  gLargeFirm1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)
	
	
preserve
	

		keep if `years'
		local controls2 geography_* year_*
		local weightmatrix "t*_c_i_share_2000*"
		local filter `years'
		
		noi Adao dsmall_dep_share1 bartikS1 `controls2' if `filter', wmatrix(`weightmatrix') tsls(gSmallFirm1)

		local se5 =string(se0[1,1], "%9.3f")
		local ciL5 =string(ciL[1,1], "%9.3f")
		local ciH5 =string(ciH[1,1], "%9.3f")

		local se6 =string(se_alpha[1,1], "%9.3f")
		local ciL6 =string(ciL2SLS[1,1], "%9.3f")
		local ciH6 =string(ciH2SLS[1,1], "%9.3f")
	restore



	
	
local years  (year>2002) & abs(gsmall_dep1)<5  & !missing(bartikGS1) & !missing(gSmallFirm1) &  cuts<4 & c_i_share_200021==0

eststo: xtivreg2 gsmall_dep1 year_* (gSmallFirm1 = bartikGS1)  if `years', fe cluster(cluster3)
drop mainsample
gen mainsample = e(sample)
local years mainsample==1
bysort geography: egen singletons = sum(mainsample)
replace mainsample=. if singletons<2
drop singletons
local years mainsample==1

*(7)

eststo: areg gsmall_dep1 gSmallFirm1 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_TableNoMiningRFS", tex append ///
	ctitle(Small Dep Gr) keep(gSmallFirm1 `controls' ) ///
	addtext(REG, OLS, F-stat, N/A, YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)
	
*(8)
eststo: areg gSmallFirm1 bartikGS1 year_*  if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable_TableNoMiningRFS", tex append ///
	ctitle(Small Dep Gr) keep(gSmallFirm1 bartikGS1 gLargeFirm1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, N/A, YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)

*(9)
eststo: xtivreg2 gsmall_dep1 year_* (gSmallFirm1 = bartikGS1)  if `years', fe cluster(cluster3)
local widstat = round(`e(widstat)',0.1)
outreg2 using "$LocalData\Tables\BartikRegTable_TableNoMiningRFS", tex append ///
	ctitle(Small Dep Gr) keep(gSmallFirm1  gLargeFirm1  `controls' ) ///
	addtext(REG, 2SLS, F-stat, `widstat', YEAR FE, YES, COUNTY FE, YES, YRS, 2003-2017)
	
	
preserve
	

		keep if `years'
		local controls2 geography_* year_*
		drop t*_c_i_share_200021
		local weightmatrix "t*_c_i_share_2000*"
		local filter `years'
		
		noi Adao dsmall_dep_share1 bartikS1 `controls2' if `filter', wmatrix(`weightmatrix') tsls(gSmallFirm1)

		local se8 =string(se_alpha[1,1], "%9.3f")
		local ciL8 =string(ciL[1,1], "%9.3f")
		local ciH8 =string(ciH[1,1], "%9.3f")

		local se9 =string(se_alpha[1,1], "%9.3f")
		local ciL9 =string(ciL2SLS[1,1], "%9.3f")
		local ciH9 =string(ciH2SLS[1,1], "%9.3f")
	restore
	*/
	
file write fh  "`se1' & `se2' & `se3' & `se4' & `se5' & `se6' & `se7' & `se8' & `se9' \\" _n
file write fh  " & [`ciL1', `ciH1'] & [`ciL2', `ciH2'] & [`ciL3', `ciH3'] & [`ciL4', `ciH4'] & [`ciL5', `ciH5'] & [`ciL6', `ciH6'] & [`ciL7', `ciH7'] & [`ciL8', `ciH8'] & [`ciL9', `ciH9']    \\" _n
	
file close fh
		
noi display "Finished running Deposit regressions."
noi etime
noi display " "


